﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageAdditionalMenuItemsController : BaseController
    {
        public IAdditionalMenuItemsRepository AdditionalMenuItemsRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageAdditionalMenuItemsController(IAdditionalMenuItemsRepository additionalMenuItemsRepository, ILanguagesRepository languagesRepository)
        {
            AdditionalMenuItemsRepository = additionalMenuItemsRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /ManageAdditionalMenuItems/

        public ViewResult Index()
        {
            var additionalMenuItems = AdditionalMenuItemsRepository.FindAllByLang(Language).OrderBy(mi => mi.Order);
            return View(additionalMenuItems);
        }

        //
        // GET: /ManageAdditionalMenuItems/Details/5

        public ViewResult Details(Int32 id)
        {
            var additionalMenuItem = AdditionalMenuItemsRepository.GetById(id);
            return View(additionalMenuItem);
        }

        //
        // GET: /ManageAdditionalMenuItems/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ManageAdditionalMenuItems/Create

        [HttpPost]
        public ActionResult Create(AdditionalMenuItem additionalMenuItem)
        {
            if (ModelState.IsValid)
            {
                additionalMenuItem.LanguageId = Language.Id;
                AdditionalMenuItemsRepository.Insert(additionalMenuItem);
                AdditionalMenuItemsRepository.Save();
                return RedirectToAction("Index");
            }

            return View(additionalMenuItem);
        }

        //
        // GET: /ManageAdditionalMenuItems/Edit/5

        public ActionResult Edit(int id)
        {
            var additionalMenuItem = AdditionalMenuItemsRepository.GetById(id);
            return View(additionalMenuItem);
        }

        //
        // POST: /ManageAdditionalMenuItems/Edit/5

        [HttpPost]
        public ActionResult Edit(AdditionalMenuItem additionalMenuItem)
        {
            var dbAdditionalMenuItem = AdditionalMenuItemsRepository.GetById(additionalMenuItem.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbAdditionalMenuItem);
                AdditionalMenuItemsRepository.Save();
                return RedirectToAction("Index");
            }
            return View(additionalMenuItem);
        }

        //
        // GET: /ManageAdditionalMenuItems/Delete/5

        public ActionResult Delete(int id)
        {
            var additionalMenuItem = AdditionalMenuItemsRepository.GetById(id);
            return View(additionalMenuItem);
        }

        //
        // POST: /ManageAdditionalMenuItems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var additionalMenuItem = AdditionalMenuItemsRepository.GetById(id);
            AdditionalMenuItemsRepository.Delete(additionalMenuItem);
            AdditionalMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        //
        // GET: /AdditionalMenuItems/IncrOrder/5

        public ActionResult IncrOrder(Int32 id)
        {
            var menuItem = AdditionalMenuItemsRepository.GetById(id);
            if (menuItem == null)
                return HttpNotFound();

            if (menuItem.Order <= 0)
                return RedirectToAction("Index");

            menuItem.Order--;
            var neighbours = AdditionalMenuItemsRepository
                .FindAllByLang(Language)
                .Where(item => item.Order == menuItem.Order)
                .Where(item => item.Id != menuItem.Id);
            foreach (var neighbour in neighbours)
                neighbour.Order++;

            AdditionalMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        //
        // GET: /AdditionalMenuItems/DecrOrder/5

        public ActionResult DecrOrder(Int32 id)
        {
            var menuItem = AdditionalMenuItemsRepository.GetById(id);
            if (menuItem == null)
                return HttpNotFound();

            menuItem.Order++;
            var neighbours = AdditionalMenuItemsRepository
                .FindAllByLang(Language)
                .Where(item => item.Order == menuItem.Order)
                .Where(item => item.Id != menuItem.Id);
            foreach (var neighbour in neighbours)
                neighbour.Order--;

            AdditionalMenuItemsRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            AdditionalMenuItemsRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}